package org.vy.dal.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Utility class for manipulating date objects.
 */
public class DateUtil {
	/** The number of milliseconds in an hour. */
	public static final long MILLISECONDS_IN_AN_HOUR = 1000L * 60 * 60;
	/** The number of milliseconds in a day. */
	public static final long MILLISECONDS_IN_A_DAY = MILLISECONDS_IN_AN_HOUR * 24;

	public static final int SECONDS_IN_A_MINUTE = 60;
	public static final int SECONDS_IN_AN_HOUR = SECONDS_IN_A_MINUTE * 60;
	public static final int SECONDS_IN_A_DAY = SECONDS_IN_AN_HOUR * 24;
	public static final int SECONDS_IN_A_YEAR = SECONDS_IN_A_DAY * 365;

	/**
	 * Returns a Date that is at midnight on the same day as the specified Date.
	 * 
	 * @param date
	 *            The Date that should be converted into midnight
	 */
	public static Date midnightize(final Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		midnightize(cal);
		return cal.getTime();
	}

	/**
	 * Set the Calendar's time to the midnight of the same day.
	 * 
	 * @param cal
	 */
	public static void midnightize(Calendar cal) {
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
	}

	/**
	 * Returns the difference in the number of days between the two specified
	 * Dates. How far the dates are into their respectives days is ignored (that
	 * is, hours, minutes, seconds, and milliseconds are zero-ed out before the
	 * comparison is made). Note that the returned value of this method is
	 * always non-negative.
	 * 
	 * @param date1
	 *            The first Date to compare.
	 * @param date2
	 *            The second Date to compare.
	 * @return The non-negative difference, in days, between the two dates
	 */
	public static int differenceInDays(final Date date1, final Date date2) {
		long firstDate = 0;
		long secondDate = 0;
		if (date1.before(date2)) {
			firstDate = midnightize(date1).getTime();
			secondDate = midnightize(date2).getTime();
		} else {
			firstDate = midnightize(date2).getTime();
			secondDate = midnightize(date1).getTime();
		}
		long l = Math.round((double) (secondDate - firstDate)
				/ MILLISECONDS_IN_A_DAY);
		return (int) l;
	}

	public static Date shiftDays(final Date date1, int days) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.setTimeInMillis(date1.getTime());
		c.add(Calendar.DATE, days);
		return c.getTime();
	}

	/**
	 * Create a Date object with the specified year, month, and date. All time
	 * values will be set to 0.
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @return Date
	 */
	public static Date newDate(int year, int month, int date) {
		return newDate(year, month, date, 0, 0, 0, 0);
	}

	/**
	 * Create a Date object with the specified values.
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param milliseconds
	 * @return Date
	 */
	public static Date newDate(int year, int month, int date, int hours,
			int minutes, int seconds, int milliseconds) {
		return newDate(year, month, date, hours, minutes, seconds,
				milliseconds, null);
	}

	/**
	 * Create a Date object with the specified values.
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param milliseconds
	 * @param timezone
	 *            String that's passed to TimeZon.getTimeZone() (e.g., "GMT")
	 * @return Date
	 */
	public static Date newDate(int year, int month, int date, int hours,
			int minutes, int seconds, int milliseconds, String timezone) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(year, month - 1, date, hours, minutes, seconds);
		c.set(Calendar.MILLISECOND, milliseconds);
		c.setTimeZone(TimeZone.getTimeZone(timezone));
		
		return c.getTime();
	}

	/**
	 * Because Timestamp.equals(Date) always returns false if the specified Date
	 * is not also a Timestamp, we must convert all Timestamp objects to Date
	 * objects.
	 */
	public static Date asDate(Date date) {
		if (date instanceof Timestamp)
			return new Date(date.getTime());
		return date;
	}

	public static Date standardDate(String str) {
		Date date = null;
		try {
			String format = "yyyy-MM-dd";
			DateFormat dateFormat = new SimpleDateFormat(format);

			date = dateFormat.parse(str);
			DateUtil.standardDate(date);
		} catch (Exception exception) {

		}
		return date;
	}

	public static String standardDate(Date date) {
		String str = null;
		try {
			
			String format = "yyyy-MM-dd";
			DateFormat dateFormat = new SimpleDateFormat(format);

			str = dateFormat.format(date);
		//	System.out.println("date : "+str);
		} catch (Exception exception) {

		}
		return str;
	}

	public static java.sql.Date getCurrentDate(){
		String date;
		java.sql.Date currentDate;
		java.util.Date currDate= new java.util.Date();
		String format = "yyyy-MM-dd";
		DateFormat dateFormat = new SimpleDateFormat(format);
		date =dateFormat.format(currDate);
		currentDate=java.sql.Date.valueOf(date);
		
		return currentDate;
	}
	public static void main(String[] args) {
		
		System.out.format("%1$-10s%2$20s",DateUtil.getCurrentDate(),"Sankalp");
//		Date a = new Date();
//		String b=a.toString();
//		String c=DateUtil.standardDate(a);
//		String d="2010-08-26";
//		System.out.println("b : "+b);
//		System.out.println("c : "+c);
//		System.out.println("d : "+d);
//		System.out.println("java : sql : "+java.sql.Date.valueOf(DateUtil.standardDate(new java.util.Date())));
//		System.out.println(new java.sql.Date(new java.util.Date().getTime()));
//  
	}
}
